sugarfreeApp.service('UploadService',['$timeout','$http',function($timeout,$http){
	  var uploadService = {};
	  uploadService.createuploader = function (uptoken_url,pickfiles,auto_start,key){
		   	  if(null==auto_start || typeof auto_start ==="undefined"){
		   		  auto_start = false;
		   	  }
		   	  var _save_key = true;
		   	  if(null===key || typeof key==="undefined"){
		   		_save_key = true;
		   	  }
			  return  Qiniu.uploader({
				  runtimes: 'html5,html4',
				  browse_button: pickfiles,
				  container: 'container',
				  drop_element: 'container',
				  max_file_size: '100mb',
				  // flash_swf_url: 'js/plupload/Moxie.swf',
				  dragdrop: true,
				  chunk_size: '4mb',
				  uptoken_url: uptoken_url,
				  domain: 'http://qiniu-plupload.qiniudn.com/',
				  save_key: _save_key,
				  auto_start: auto_start,
				  init: {
					  'FilesAdded': function(up, files) {
						  console.log(up.id+"add one file。。。");
						  plupload.each(files, function(file) {
							  $timeout(function() { 
								  file.nativePath = window.URL.createObjectURL(file.getNative());
							  }, 1);
						  });
					  },
					  'BeforeUpload': function(up, file) {
						  console.log("BeforeUpload");
					  },
					  'UploadProgress': function(up, file) {
						  console.log('UploadProgress',up,file);
					  },
					  'UploadComplete': function() {
						  console.log('UploadComplete');
					  },
					  'FileUploaded': function(up, file, info) {
						  console.log("up",up);
						  if(null!==up.flage && up.flage === "remove"){
							  up.removeFile(file);
						  }
						  console.log('FileUploaded');
					  },
					  'Error': function(up, err, errTip) {
					  	console.info('err',err,errTip);
						  console.log('Error');
					  },
					  'Key': function(up, file) {
				            var key = key;
				            // do something with key here
				            return key;
				        }
				  }
			  });
	  };
	  
	  uploadService.delImage=function($scope,fileid){
		  plupload.each($scope.uploader.files, function(file) {
		    	if(file !==undefined &&file.id===fileid){
		    		$scope.uploader.removeFile(file);
		    		return;
		    	}
		  });
	  };
	  return uploadService;
}]);